package com.yiwei.ydd.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.activity.MyRefereeActivity;
import com.yiwei.ydd.activity.WebViewActivity;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.PagesDetailBean;
import com.yiwei.ydd.api.bean.SetInviteBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.constant.Const;
import com.yiwei.ydd.event.RegisterSuccessEvent;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/4/24.
 */

public class InviteFriendDialog extends Dialog {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.edit_invite)
    EditText editInvite;
    @BindView(R.id.btn_change_phone)
    TextView btnChangePhone;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.btn_skip)
    TextView btnSkip;
    @BindView(R.id.btn_agreement)
    TextView btnAgreement;
    private Context context;


    public InviteFriendDialog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public InviteFriendDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_register_invite);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = (int) (Util.getDisplayMetrics(context).widthPixels);
        mWindow.setAttributes(lp);


    }


    public void setData() {
//        adapter.update();
    }


    private DialogDefaultClickListener listener;

    public void setDialogClickListener(DialogDefaultClickListener listener) {
        this.listener = listener;
    }

    @OnClick({R.id.btn_agreement, R.id.btn_back, R.id.btn_submit, R.id.btn_skip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
            case R.id.btn_skip:
                com.yiwei.ydd.util.Dialog.showDefaultDialog(context, "", "填写邀请码，好友将获得佣金，您确定要跳过吗？", "跳过", "继续填写", false, new com.yiwei.ydd.util.Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {

                    }

                    @Override
                    public void cancel() {
                        EventBus.getDefault().post(new RegisterSuccessEvent());
                        dismiss();
                    }
                });
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editInvite))) {
                    ToastUtil.makeText(context, "邀请码不能为空");
                    return;
                }
                getSetInvite(UI.toString(editInvite));
                break;
            case R.id.btn_agreement:
                getPagesDetail();
//                new PagesDetailDialog(context).setKeyword(Const.PageDetailType.APP_SERVICE).show();
                break;

        }
    }
    public void getPagesDetail() {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }
        ((BaseActivity) context).loadingDialog.show();
        PagesDetailBean item = new PagesDetailBean();
        item.keyword = Const.PageDetailType.APP_SERVICE;
        Api.api_service.getPagesDetail(item).compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        ((BaseActivity) context).loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Intent intent1 = new Intent(context, WebViewActivity.class);
                    intent1.putExtra("content", result.datas.content);
                    intent1.putExtra("title", result.datas.title);
                    context.startActivity(intent1);
                });
    }

    public void getSetInvite(String code) {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }

        ((BaseActivity) context).loadingDialog.show();
        SetInviteBean bean = new SetInviteBean();
        bean.invite_code = code;
        Api.api_service.getSetInvite(bean).compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        ((BaseActivity) context).loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(context, "设置成功");
                    EventBus.getDefault().post(new RegisterSuccessEvent());
                });
    }

    public interface DialogDefaultClickListener {
        public abstract void confirm();

        public abstract void cancel();
    }
}
